<!--  -->
<template>
  <div
    id="cm-modal"
    v-if="value"
    :style="{
      '--width': transOrigin.width,
      '--height': transOrigin.height,
      '--top': transOrigin.top,
      '--left': transOrigin.left,
    }"
  >
    <div class="shadow" @click="bodyClose"></div>
    <div class="cm-modal-container" @click.stop
    :class="[showingup?'cm-modal-showup':'cm-modal-disappear']"
    :style="{
      '--wid': width,
      '--hei': height,
    }"
    >
      <div class="cm-modal-content" v-show="visibility">
        <slot></slot>
        <!-- 用户自定义弹窗内容区域 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visibility: false,
      transOrigin: "",
      showingup:true,

    };
  },

  components: {},

  props: ["value",'bodyClosable','width','height'],

  computed: {},

  mounted() {
    document.addEventListener("click", this.handleClick);
  },

  methods: {
    handleClick(e) {
      let rect = e.target.getBoundingClientRect();
      this.transOrigin = rect;
      console.log(rect);
    },
    bodyClose(){//点击除自身外部分关闭弹窗
      this.showingup = false;
      const that = this
      that.visibility=false
      console.log('bodyClose')
      if(this.bodyClosable == true){
        setTimeout(function(){
          document.addEventListener("click", that.handleClick);
          that.$emit('bodyClose')
        },300)

      }
    },
    newClickListener(){
      console.log('newClickListener')
    }
  },

  destroyed() {
    document.removeEventListener("click", this.handleClick);
  },

  watch: {
    value(newval) {
      const that = this;
      if (newval == true) {
        this.showingup = true;
        setTimeout(function () {
          that.visibility = newval;
          document.removeEventListener("click",that.handleClick)
        }, 300);
      }
      else{
        that.visibility=false
        this.showingup = false;
      }
    },
  },
};
</script>
<style>
#cm-modal {
  width: 100vw;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 10000;
}
.cm-modal-container {
  position: absolute;
  width: calc(var(--wid) * 1px);
  height: calc(var(--hei) * 1px);
  transform: translateY(-50%) translateX(-50%);
  margin: 0 auto;
  background-color: white;
  top: 50%;
  left: 50%;
  box-shadow: rgba(0, 0, 0, 0.25) 0px 25px 50px -12px;
  border-radius: 4px;
  z-index: 100001;
  display: flex;
  flex-wrap: wrap;
  transition: all 0.3s;
  overflow: scroll
}
.cm-modal-showup{
  animation: confirmwindowShow .3s ease;
}
.cm-modal-disappear{
  animation: confirmwindowDis .3s ease;
}
@keyframes confirmwindowShow {
  0% {
    left: calc(var(--left) * 1px);
    top: calc(var(--top) * 1px);
    transform: translateX(-50%) translateY(-50%) scale(0);
    /* width: calc(var(--width) * 1px);
    height: calc(var(--height) * 1px); */
  }
  100% {
    transform: translateX(-50%) translateY(-50%) scale(1);
    left: 50%;
    top: 50%;
  }
}
@keyframes confirmwindowDis {
  to{
    left: calc(var(--left) * 1px);
    top: calc(var(--top) * 1px);
    width: calc(var(--width) * 1px);
    height: calc(var(--height) * 1px);
  }
}
#cm-modal > .shadow {
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(132, 132, 132, 0.333);
  height: 100vh;
  width: 100vw;
  z-index: 9999;
}
.cm-modal-content{
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
